local hook_list = {}
local function hookAnim(self, anim)
    if hook_list[self][anim] then
        return hook_list[self][anim]
    else
        print("need_to_redir_anim:", anim)
        return hook_list[self][anim] or hook_list[self].default
    end
end
local oldPlayAnimation = AnimState.PlayAnimation
function AnimState:PlayAnimation(anim, loop, ...)
    if hook_list[self] then
        anim = hookAnim(self, anim)
    end
    return oldPlayAnimation(self, anim, loop, ...)
end

local oldSetBankAndPlayAnimation = AnimState.SetBankAndPlayAnimation
function AnimState:SetBankAndPlayAnimation(bank, anim, loop, ...)
    if hook_list[self] then
        anim = hookAnim(self, anim)
    end
    return oldSetBankAndPlayAnimation(self, bank, anim, loop, ...)
end

local oldPushAnimation = AnimState.PushAnimation
function AnimState:PushAnimation(anim, loop, ...)
    if hook_list[self] then
        anim = hookAnim(self, anim)
    end
    return oldPushAnimation(self, anim, loop, ...)
end

local oldSetFrame = AnimState.SetFrame
function AnimState:SetFrame(f, ...)
    if hook_list[self] then
        f = math.min(f, math.max(0, self:GetCurrentAnimationNumFrames() - 1))
    end
    return oldSetFrame(self, f, ...)
end

function GLOBAL.Lambris_RegisterAnimStateHook(inst, animlist)
    local animstate = inst.AnimState
    if type(animstate) == "userdata" then
        hook_list[animstate] = animlist
    else
        animstate = inst.userdatas and inst.userdatas.AnimState
        if animstate then
            hook_list[animstate] = animlist
        else
            error("Lambris:意外状况 模组冲突 请联系作者")
        end
    end
end

function GLOBAL.Lambris_UnRegisterAnimStateHook(inst)
    local animstate = inst.AnimState
    if type(animstate) == "userdata" then
        hook_list[animstate] = nil
    else
        animstate = inst.userdatas and inst.userdatas.AnimState
        if animstate then
            hook_list[animstate] = nil
        else
            error("Lambris:意外状况 模组冲突 请联系作者")
        end
    end
end
